using System.Collections;
using UnityEngine;

public class ResManager : SingtonMono<ResManager>
{
    //同步加载
    public T LoadRes<T>(string name) where T : Object { 

        T res = Resources.Load<T>(name);
        //如果对象是GameObj
        if (res is GameObject)
            return (T)Instantiate(res);
        else
            return res;
    }

    //异步加载
    public void LoadResAsync<T>(string name, System.Action<T> callback) where T : Object {
        MonoManager.Instance.StartCoroutine(LoadResIE(name, callback));
    }

    private IEnumerator LoadResIE<T>(string name, System.Action<T> callback) where T : Object
    {
        ResourceRequest request = Resources.LoadAsync<T>(name);
        yield return request;

        if (request.asset is GameObject)
            callback(Instantiate(request.asset) as T);
        else
            callback(request.asset as T);
    }
       
}
